﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace Vehicle.Web.Pages.Vehicles
{
    /// <summary>
    /// Copyright (C) M-Tear团队
    /// 文 件 名：Vehicles_Edit.aspx.cs
    /// 版    本：v1.00.0000
    /// 创建标识：2009-08-28   Created by 杨光远 V1.00.0000
    /// 功能说明：编辑车辆信息页面类
    /// 注意事项：无
    /// 
    /// 更新记录：
    /// 
    /// </summary>
    public partial class Vehicles_Edit : Component.PageBase
    {
        #region ---基本变量实例化---
        Vehicle.BLL.Vehicles b_Vehicles = new Vehicle.BLL.Vehicles();//定义逻辑层变量
        BLL.Log b_Log = new Vehicle.BLL.Log();
        Vehicle.BLL.Motorcade b_Motorcade = new Vehicle.BLL.Motorcade();//定义逻辑层变量
        Model.VehiclesInfo m_VehiclesInfo = new Vehicle.Model.VehiclesInfo();//实例化实体层
        Vehicle.BLL.Pilot b_Pilot = new Vehicle.BLL.Pilot();//定义逻辑层变量
        #endregion

        #region ---基本方法---
        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                VehiclesDataBind();//绑定车辆信息
            }
        }


        /// <summary>
        /// 根据车队绑定驾驶员
        /// </summary>
        public void PilodDataBind()
        {
            b_Pilot.DropDownListBind(drop_P_ID, new Vehicle.Model.ComponentsInfo("Pilot", "M_ID", drop_M_ID.SelectedValue, "V_ID", "0", "P_IsDelete", "1", "P_ID"));//绑定驾驶员

        }
        /// <summary>
        /// 绑定车辆方法
        /// </summary>
        public void VehiclesDataBind()
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"] != "")
            {


                DataTable dt = b_Motorcade.GetModel(new Vehicle.Model.ComponentsInfo("Vehicles", "V_ID", Request.QueryString["id"].ToString(), "V_ID"));
                if (dt.Rows.Count > 0)
                {
                    b_Motorcade.DropDownListBind(drop_M_ID);//绑定车辆
                    PilodDataBind();//绑定驾驶员
                    drop_M_ID.SelectedValue = dt.Rows[0]["M_ID"].ToString();//绑定车辆名称
                    drop_P_ID.SelectedValue = dt.Rows[0]["P_ID"].ToString();//绑定所属驾驶员
                    txt_V_LicenseNum.Text = dt.Rows[0]["V_LicenseNum"].ToString();//绑定车牌号
                    hf_V_LicenseNum.Value = dt.Rows[0]["V_LicenseNum"].ToString();//绑定车牌号
                    txt_V_ProvinceCity.Text = dt.Rows[0]["V_ProvinceCity"].ToString();//绑定所属省市
                    txt_V_Length.Text = dt.Rows[0]["V_Length"].ToString();//绑定车辆长度
                    txt_V_Load.Text = dt.Rows[0]["V_Load"].ToString();//绑定车辆载重量
                    txt_V_Model.Text = dt.Rows[0]["V_Model"].ToString();//绑定车辆类型
                    txt_V_Route.Text = dt.Rows[0]["V_Route"].ToString();//绑定车辆路线
                    txt_V_Time.Text = dt.Rows[0]["V_Time"].ToString();//绑定车辆入录时间
                    Gmdp_V_ETiem.Date = Common.TextHelper.StringToDateTime(dt.Rows[0]["V_ETiem"].ToString());//绑定车辆入录时间
                    drop_V_Transit.SelectedValue = dt.Rows[0]["V_Transit"].ToString();//绑定当前是否有任务
                    txt_V_Note.Text = dt.Rows[0]["V_Note"].ToString();//绑定车辆备注信息
                    hf_V_LastTime.Value = dt.Rows[0]["V_LastTime"].ToString();//绑定车辆最后修改时间
                    txt_V_Unit.Text = dt.Rows[0]["V_Unit"].ToString();//绑定车辆保险单位
                    txt_V_Num.Text = dt.Rows[0]["V_Num"].ToString();//绑定车辆编号
                    hf_P_ID.Value = dt.Rows[0]["P_ID"].ToString();//绑定所属驾驶员
                    txt_V_MNumber.Text = dt.Rows[0]["V_MNumber"].ToString();//车辆出厂编号
                    Gmdp_V_ReleaseDate.Date = Common.TextHelper.StringToDateTime(dt.Rows[0]["V_ReleaseDate"].ToString());//绑定车辆出厂日期
                    Gmdp_V_BuyDate.Date = Common.TextHelper.StringToDateTime(dt.Rows[0]["V_BuyDate"].ToString());//绑定购买车辆日期
                    txt_V_Fuel.Text = dt.Rows[0]["V_Fuel"].ToString();//绑定车辆燃料种类
                    txt_V_Let.Text = dt.Rows[0]["V_Let"].ToString();//绑定车辆排放标准
                    txt_V_Manufacturer.Text = dt.Rows[0]["V_Manufacturer"].ToString();//绑定车辆生产厂家
                    txt_V_Color.Text = dt.Rows[0]["V_Color"].ToString();//绑定车辆颜色
                    if (dt.Rows[0]["P_ID"].ToString() != "0")
                    {
                        drop_M_ID.Enabled = false;
                        drop_P_ID.Enabled = false;
                        drop_P_ID.Items.Add(new ListItem(b_Vehicles.GetModel(new Vehicle.Model.ComponentsInfo("pilot", "P_ID", dt.Rows[0]["P_ID"].ToString(), "P_ID")).Rows[0]["P_Name"].ToString(), dt.Rows[0]["P_ID"].ToString()));
                        drop_P_ID.SelectedValue = dt.Rows[0]["P_ID"].ToString();  //默认选择当前的车辆
                    }

                }
                else
                {
                    Common.MessageHelper.AlertBack("该记录已删除!");
                }
            }
            else
            {
                Common.MessageHelper.AlertBack("您的操作有误,请重新尝试!");
            }
        }
        /// <summary>
        /// 修改数据逻辑判断是否有数据更新事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"] != "")
            {
                if (hf_Set.Value == "1")//取值判断
                {
                    //提交前判断数据是否有被改变
                    if (b_Motorcade.GetModel(new Vehicle.Model.ComponentsInfo("Vehicles", "V_ID", Request.QueryString["id"].ToString(), "V_ID")).Rows[0]["V_LastTime"].ToString() == hf_V_LastTime.Value)
                    {
                        Repeat();
                    }
                    else
                    {
                        d_Show.Visible = true;
                        hf_Set.Value = "0";
                    }
                }
                else
                {
                    Repeat();
                }
            }
            else
            {
                Common.MessageHelper.AlertBack("您的操作有误,请重新尝试!");
            }
        }
        /// <summary>
        /// 逻辑判断是否有编号重复
        /// </summary>
        public void Repeat()
        {
            //判断当前输入的车牌是否和之前的车牌相同,不相同则先判断是否有重复的车牌
            if (txt_V_LicenseNum.Text.Trim() != hf_V_LicenseNum.Value.ToString())
            {
                //添加前判断是否已经有该车牌的车队
                if (b_Motorcade.GetModel(new Vehicle.Model.ComponentsInfo("Vehicles",  "V_LicenseNum", txt_V_LicenseNum.Text.Trim(),"1","1","V_IsDelete", "1", "V_ID")).Rows.Count == 0)
                {
                    UpdateDate();
                }
                else
                {
                    Common.MessageHelper.Alert("已经有相同车牌号,请重新填写");
                }
            }
            else
            {
                UpdateDate();
            }
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        protected void UpdateDate()
        {
            m_VehiclesInfo.V_ID = Common.TextHelper.StringToInt(Request.QueryString["id"].ToString());//绑定车辆ID
            m_VehiclesInfo.M_ID = Common.TextHelper.StringToInt(drop_M_ID.SelectedValue);//所属车辆编号
            m_VehiclesInfo.P_ID = Common.TextHelper.StringToInt(drop_P_ID.SelectedValue);//所属驾驶员编号
            m_VehiclesInfo.V_LicenseNum = txt_V_LicenseNum.Text.Trim();//车牌号
            m_VehiclesInfo.V_ProvinceCity = txt_V_ProvinceCity.Text.Trim();//所属省市
            m_VehiclesInfo.V_Length = txt_V_Length.Text.Trim();//车辆长度
            m_VehiclesInfo.V_Load = Common.TextHelper.StringTofloat(txt_V_Load.Text.Trim());//车辆载重量
            m_VehiclesInfo.V_Model = txt_V_Model.Text.Trim();//车辆类型
            m_VehiclesInfo.V_Route = txt_V_Route.Text.Trim();//车辆路线
            m_VehiclesInfo.V_Unit = txt_V_Unit.Text.Trim();//车辆保险单位
            m_VehiclesInfo.V_Time = Common.TextHelper.StringToDateTime(txt_V_Time.Text.Trim());//车辆录入时间
            m_VehiclesInfo.V_Num = txt_V_Num.Text.Trim();//车辆编号
            m_VehiclesInfo.V_ETiem = Common.TextHelper.StringToDateTime(Gmdp_V_ETiem.DateString);//车辆到期时间
            m_VehiclesInfo.V_Transit = Common.TextHelper.StringToInt(drop_V_Transit.SelectedValue);//是否有承运任务
            m_VehiclesInfo.V_Note = Common.TextHelper.CheckStr(txt_V_Note.Text);//车辆信息备注
            m_VehiclesInfo.V_LastTime = DateTime.Now;//车辆最后修改时间默认添加为当前时间
            m_VehiclesInfo.V_IsDelete = 0;//是否删除 默认0 不删除
            m_VehiclesInfo.V_MNumber = this.txt_V_MNumber.Text.Trim();//车辆出厂编号
            m_VehiclesInfo.V_ReleaseDate = Common.TextHelper.StringToDateTime(this.Gmdp_V_ReleaseDate.DateString);//车辆出厂日期
            m_VehiclesInfo.V_BuyDate = Common.TextHelper.StringToDateTime(this.Gmdp_V_BuyDate.DateString);//购买车辆日期
            m_VehiclesInfo.V_Fuel = this.txt_V_Fuel.Text.Trim();//车辆燃料种类
            m_VehiclesInfo.V_Let = this.txt_V_Let.Text.Trim();//车辆排放标准
            m_VehiclesInfo.V_Manufacturer = this.txt_V_Manufacturer.Text.Trim();//车辆生产厂家
            m_VehiclesInfo.V_Color = this.txt_V_Color.Text.Trim();//车辆颜色
            b_Vehicles.Update(m_VehiclesInfo);
            if (hf_P_ID.Value != drop_P_ID.SelectedValue)
            {
                b_Vehicles.AllUpdate(new Vehicle.Model.ComponentsInfo("Pilot", "V_ID", "0", "V_ID", hf_P_ID.Value));//更改原先驾驶员表
                b_Vehicles.AllUpdate(new Vehicle.Model.ComponentsInfo("Pilot", "V_ID", Request.QueryString["id"].ToString(), "P_ID", drop_P_ID.SelectedValue));//更改当前驾驶员
            }
            b_Log.Add(new Vehicle.Model.LogInfo(session_ID, DateTime.Now, "成功修改编号为" + txt_V_Num.Text.Trim() + "的车辆信息"));//添加日志
            Common.MessageHelper.AlertRedirect("车辆信息修改成功", "Main.aspx");
        }

        /// <summary>
        /// 车队下拉菜单事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void drop_M_ID_SelectedIndexChanged(object sender, EventArgs e)
        {
            PilodDataBind();
        }

        #endregion
    }
}